package com.mzkj.wanjulaile.mobile.api.modules.account.service;

import com.google.common.collect.Maps;
import com.mzkj.common.utils.Collections3;
import com.mzkj.wanjulaile.mobile.api.common.service.BaseService;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Baby;
import com.mzkj.wanjulaile.mobile.api.modules.account.mapper.BabyMapper;
import com.mzkj.wanjulaile.mobile.api.modules.category.entity.SubCategory;
import com.mzkj.wanjulaile.mobile.api.modules.toy.entity.Toy;
import com.mzkj.wanjulaile.mobile.api.modules.toy.mapper.ToyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * accountService
 * Created by wangxw on 2016/12/12.
 */
@Service("babyService")
public class BabyServiceImpl extends BaseService implements BabyService {

    @Autowired
    private BabyMapper babyMapper;
    @Autowired
    private ToyMapper toyMapper;

    public List<Baby> getListByAccountId(String accountId){
        return babyMapper.getListByAccountId(accountId);
    }

    @Transactional(readOnly = false)
    public void create(Baby baby) {
        Date date = new Date();
        baby.setCreateDate(date);
        baby.setUpdateDate(date);
        babyMapper.insert(baby);
    }

    @Transactional(readOnly = false)
    public void update(Baby baby){
        babyMapper.update(baby);
    }


    public Baby get(String id){
       return babyMapper.get(id);
    }

    public String getBabyAbilitiesByAge(int babyMonths) {
        return babyMapper.getBabyAbilitiesByAge(babyMonths);
    }
    /**
     * 没有宝宝信息推荐的玩具
     * @param baby
     * @param toy
     * @param babyToy
     * @return
     */
    @Override
    public Map<String, Object> getRecommendNoBaby(Baby baby, Toy toy, Map<String, Object> babyToy) {
        int pageSize = 8;
        Map<String, String> pageMap = Maps.newHashMap();
        pageMap.put("limit", "limit 0 , " + pageSize);
        toy.setSqlMap(pageMap);
        toy.setSorts("4");
        getRecommendToysNoBaby(toy, babyToy);
        babyToy.put("baby", baby);
        babyToy.put("toySearch", toy);
        return babyToy;
    }
    private void getRecommendToysNoBaby(Toy toy, Map<String, Object> babyToy) {
        List<Toy> homeToyList = toyMapper.findToyList(toy);
        List<Toy> homeRecommend1 = new ArrayList<Toy>();
        List<Toy> homeRecommend2 = new ArrayList<Toy>();
        if (!Collections3.isEmpty(homeToyList)){
            for (int i = 0; i < 4; i++) {
                Toy toy1 = homeToyList.get(i);
                setSubCategoryList(toy1);
                homeRecommend1.add(toy1);
            }
            for (int i = 4; i < 8; i++) {
                Toy toy2 = homeToyList.get(i);
                setSubCategoryList(toy2);
                homeRecommend2.add(toy2);
            }
            babyToy.put("recommendListOne",homeRecommend1);
            babyToy.put("recommendListTwo",homeRecommend2);
        }
    }

    private void setSubCategoryList(Toy toy) {
        List<SubCategory> subCategories = toy.getSubCategories();
        List<SubCategory> subList = new ArrayList<SubCategory>();
        if (!Collections3.isEmpty(subCategories)) {
            for (SubCategory temp : subCategories) {
                if (null != temp.getType()) {
                    subList.add(temp);
                }
            }
        }
        toy.setSubCategories(subList);
    }

}
